package org.sunears;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.*;

public class MainApp {

    /**
     * ImageMagick的路径
     */
    public static String imageMagickPath = null;
    public static String rootPath="";
    static {
        /**
         *
         * 获取ImageMagick的路径
         */

        //linux下不要设置此值，不然会报错
        imageMagickPath = "d:\\ImageMagick-7.0.8-Q8";

        rootPath=System.getProperty("user.dir");
    }
    /**
     *
     * 根据坐标裁剪图片
     *
     * @param srcPath   要裁剪图片的路径
     * @param newPath   裁剪图片后的路径
     * @param x         起始横坐标
     * @param y         起始纵坐标
     * @param x1        结束横坐标
     * @param y1        结束纵坐标
     */

    public static void cutImage(String srcPath, String newPath, int x, int y, int x1,	int y1) throws Exception {
        int width = x1 - x;
        int height = y1 - y;
        IMOperation op = new IMOperation();
        op.addImage(srcPath);
        /**
         * width：  裁剪的宽度
         * height： 裁剪的高度
         * x：       裁剪的横坐标
         * y：       裁剪的挫坐标
         */
        op.crop(width, height, x, y);
        op.addImage(newPath);
        ConvertCmd convert = new ConvertCmd();

        // linux下不要设置此值，不然会报错
        convert.setSearchPath(imageMagickPath);

        convert.run(op);
    }

    /**
     *
     * 根据尺寸缩放图片
     * @param width             缩放后的图片宽度
     * @param height            缩放后的图片高度
     * @param srcPath           源图片路径
     * @param newPath           缩放后图片的路径
     */
    public static void cutImage(int width, int height, String srcPath,	String newPath) throws Exception {
        IMOperation op = new IMOperation();
        op.addImage(srcPath);
        op.resize(width, height);
        op.addImage(newPath);
        ConvertCmd convert = new ConvertCmd();
        // linux下不要设置此值，不然会报错
        convert.setSearchPath(imageMagickPath);
        convert.run(op);

    }
    public static void testCavas(String srcPath,String newPath){
        IMOperation op=new IMOperation();
        //convert -size 100x100 canvas:khaki  canvas_khaki.gif
//        op.size(100,100);
//        op.addRawArgs("canvas:khaki");
//        op.addImage(rootPath+"\\canvastest.gif");

        //  convert -background lightblue -fill blue \
        //          -font Candice -pointsize 72 label:Anthony \
        //          label.gif
//
//        op.background("lightblue").fill("blue").font(rootPath+"\\msyhbd.ttc").pointsize(72);
//        op.addImage("label:可以");
//        op.addImage(rootPath+"\\Label.gif");

//        op.addImage(srcPath);
//        op.background("lightblue").fill("blue").font(rootPath+"\\msyhbd.ttc").pointsize(72);
//        op.addImage("label:@text.utf8");
//        op.addImage(rootPath+"\\Label.gif");
//        op.addImage(newPath);

        //  convert -background lightblue  -fill blue  -font Corsiva -pointsize 36 \
        //          -size 320x   caption:'This is a very long caption line.' \
        //          caption.gif
        op.background("lightblue").fill("blue").font(rootPath+"\\msyhbd.ttc").pointsize(36).size(420,100).interwordSpacing(1.5);

        op.addImage("caption:@text.utf8");
        op.addImage(rootPath+"\\caption.png");
//        op.addImage("canvas:none");
//        op.addRawArgs(" -background lightblue -fill blue -font Arial -pointsize 72 label:Anthony ",rootPath+"\\canvatest.png");
//convert template.png -compose over 44.jpg -geometry 280x280+181+623 -composite new.png
//convert new.png -fill white -font /usr/share/fonts/windows/msyh.ttf -pointsize 24 -annotate +269+198 "我是XXXXX" new_label_middle.png
//convert new_label_middle.png -fill white -font /usr/share/fonts/windows/msyh.ttf -pointsize 24 -annotate +269+240 "我为XXXXX代言" new_label_middle2.png
        ;
//        op.addRawArgs("E:\\imagemagickjava\\2:");
//        op.fill("white").font(rootPath+"\\msyi.ttf").pointsize(36).annotate(0);
//        op.addImage("我是XXXXX");
//        op.addImage(newPath);
        ConvertCmd convert = new ConvertCmd();
        // linux下不要设置此值，不然会报错
        convert.setSearchPath(imageMagickPath);
        System.out.println(op.toString());
        try {

            convert.run(op);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IM4JavaException e) {
            e.printStackTrace();
        }
    }
    public static void pangoTest(String srcPath,String desPath){

        //convert -gravity center pango:@pango_test.txt  pango_test.png
        IMOperation op = new IMOperation();
        op.gravity("center");
        op.addImage("pango:@panggotest.txt");
        op.addImage(rootPath+"\\pango_text.png");
        ConvertCmd convert = new ConvertCmd();
        // linux下不要设置此值，不然会报错
        convert.setSearchPath(imageMagickPath);
        System.out.println(op.toString());
        try {

            convert.run(op);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IM4JavaException e) {
            e.printStackTrace();
        }
    }
    /**
     * 测试拼音
     * @param srcPath desPath
     */
    public static void pinyinTest(String srcPath,String desPath){
        IMOperation op = new IMOperation();
        op.addImage(srcPath);
        op.font(rootPath+"\\msyhbd.ttc").pointsize(70).stroke("#fff0").strokewidth(2).annotate(0,0,80,120);
        op.addImage("问题：");
        op.font(rootPath+"\\msyhbd.ttc").pointsize(70).stroke("none").fill("#7b2d2d").annotate(0,0,80,120);
        op.addImage("问题：");
        //é     é     é
        //鹅，  鹅，  鹅 ，
        //qū xiàng xiàng tiān gē
        //曲   项    向   天   歌 。
        //bái máo fú lǜ shuǐ
        //白 毛 浮 绿 水 ，
        //hōng zhǎng bō qīng bō
        //红 掌 拨 清 波 。
        String [] as1={"qū","曲"};
        String [] as2={"xiàng", "项"};
        String [] as3={"xiàng","向"};
        String [] as4={"tiān","天"};
        String [] as5={"gē","歌 。"};
        List<String[]> py= new ArrayList<>();
        py.add(as1);
        py.add(as2);
        py.add(as3);
        py.add(as4);
        py.add(as5);
//        String[][] arr1 = {{"qū","曲"}, {"xiàng", "项"}, {"xiàng","向"},{"tiān","天"},{"gē","歌 。"}};
        int fsize=0;
        int[] arraysize=new int[]{80,180,280,380,480};
        for(int i=0;i<py.size();i++){
            op.font(rootPath+"\\msyhbd.ttc").pointsize(30).stroke("#fff0").strokewidth(2).annotate(0,0,arraysize[i],240);
            op.addImage(py.get(i)[0]);
            op.font(rootPath+"\\msyhbd.ttc").pointsize(46).stroke("none").fill("#7b2d2d").annotate(0,0,arraysize[i],300);
            op.addImage(py.get(i)[1]);
        }



        op.addImage(desPath);



//

//        op.addImage("canvas:none");
//        op.size(320,85).font("Arial").pointsize(72).draw("\"text 25,60 \'magickf\'\"");
//        op.addImage(rootPath+"\\f.png");


        ConvertCmd convert = new ConvertCmd();

        // linux下不要设置此值，不然会报错
        convert.setSearchPath(imageMagickPath);
//        convert.setCommand("convert -size 320x85 canvas:none -font Arial -pointsize 72 -draw \"text 25,60 'magickfff'\" f.png");

        try {
            convert.run(op);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IM4JavaException e) {
            e.printStackTrace();
        }
        System.out.println(op.toString());
//        convert.run(op, srcPath, orgPath);
    }
    /**
     * 给图片加水印
     * @param srcPath            源图片路径
     */
    public static void addImgText(String srcPath,String orgPath) throws Exception {
        IMOperation op = new IMOperation();
//        op.font(rootPath+"\\msyh.ttc").stroke("black").strokewidth(1).gravity("center").pointsize(50).fill("green").interwordSpacing(10.0)
//                .draw("text 20,20 \'日 出 而 作，日 入 而 息。\'");
//        op.font(rootPath+"\\msyh.ttc").stroke("black").strokewidth(1).gravity("south").pointsize(30).fill("green").interwordSpacing(10.0)
//                .draw("text 20,20 \'凿井而饮，耕田而食。\'");
//        op.font("宋体").pointsize(50).draw("gravity center  fill white text 0,12 'Copyright'");
//        "  convert logo.jpg  -font Arial -pointsize 20 \\\n" +
//                "          -draw \"gravity south \\\n" +
//                "                 fill black  text 0,12 'Copyright' \\\n" +
//                "                 fill white  text 1,11 'Copyright' \" \\\n" +
//                "          wmark_text_drawn.jpg"


//        op.font("宋体").pointsize(50).draw("gravity center fill black text  'F' fill white  text 1,11 'F'");
//        op.gravity("northwest").annotate(0);
        //
        //convert dragon.gif -gravity south \
        //          -stroke '#000C' -strokewidth 2 -annotate 0 'Faerie Dragon' \
        //          -stroke  none   -fill white    -annotate 0 'Faerie Dragon' \
        //          anno_outline.jpg
        //直接从文件读
        op.addImage(srcPath);
        op.font(rootPath+"\\msyhbd.ttc").pointsize(70).stroke("#fff0").strokewidth(2).annotate(0,0,80,120);
        op.addImage("问题：");
        op.font(rootPath+"\\msyhbd.ttc").pointsize(70).stroke("none").fill("#7b2d2d").annotate(0,0,80,120);
        op.addImage("问题：");
        op.font(rootPath+"\\msyhbd.ttc").pointsize(46).gravity("center").stroke("#fff0").strokewidth(2).annotate(0);

        op.addImage("@text.utf8");
        op.font(rootPath+"\\msyhbd.ttc").pointsize(46).stroke("none").fill("#7b2d2d").annotate(0);

        op.addImage("@text.utf8");
//        -draw "gravity south \
//        fill black  text 0,12 'Copyright' \
 //       fill white  text 1,11 'Copyright' " \
//        op.addImage("NorthWest");//可以添加字符串就是占位符
        op.addImage(orgPath);



//

//        op.addImage("canvas:none");
//        op.size(320,85).font("Arial").pointsize(72).draw("\"text 25,60 \'magickf\'\"");
//        op.addImage(rootPath+"\\f.png");


        ConvertCmd convert = new ConvertCmd();

        // linux下不要设置此值，不然会报错
        convert.setSearchPath(imageMagickPath);
//        convert.setCommand("convert -size 320x85 canvas:none -font Arial -pointsize 72 -draw \"text 25,60 'magickfff'\" f.png");

        convert.run(op);
        System.out.println(op.toString());
//        convert.run(op, srcPath, orgPath);
    }

    /*
     * 中文转unicode编码
     */
    public static String gbEncoding(final String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int i = 0; i < utfBytes.length; i++) {
            String hexB = Integer.toHexString(utfBytes[i]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }
    /*
     * unicode编码转中文
     */
    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }
    public static void main(String[] args) {
        System.out.println(System.getProperty("user.dir"));
        String path=System.getProperty("user.dir");
        try {
//            addImgText(path+"\\1.png",path+"\\2.png");
//            testCavas(path+"\\1.png",path+"\\2.png");
//            pangoTest(path+"\\1.png",path+"\\2.png");
            pinyinTest(path+"\\1.png",path+"\\2.png");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("hello");


    }
}
